package tako.wisers.com.android.apis.activity;

import tako.wisers.com.android.apis.R;
import tako.wisers.com.android.apis.event.Event;
import tako.wisers.com.android.apis.event.EventBus;
import tako.wisers.com.android.apis.event.WebEvent;
import tako.wisers.com.android.apis.handler.Handler;
import tako.wisers.com.android.apis.item.NewsItem;
import tako.wisers.com.android.apis.util.Global;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class NewsAbstract extends Activity {
	private EventBus mbus;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news_abstr);
		mbus = EventBus.getInstance();
		Intent intent = getIntent();
		NewsItem item = intent.getParcelableExtra(Global.ITEM_KEY);
		String publicher = item.getOwner();
        String title = item.getTitle();
        String date = item.getDate();
        String snippet = item.getContent() == null ? "" : item.getContent();
        String url = item.getURL();
        Bitmap image = item.getBitmap();
        TextView titletext = (TextView) findViewById(R.id.headline);
        TextView publichertext = (TextView) findViewById(R.id.source);
        TextView datetext = (TextView) findViewById(R.id.date);
        TextView snippettext = (TextView) findViewById(R.id.snittep);
        titletext.setText(title);
        publichertext.setText(publicher);
        datetext.setText(date);
        snippettext.setText(Html.fromHtml(snippet));
        snippettext.setMovementMethod(LinkMovementMethod.getInstance());
        ImageView imageView = (ImageView) findViewById(R.id.news_image);
        if (image != null) {
			imageView.setImageBitmap(image);
			imageView.setVisibility(View.VISIBLE);
		}else{
			imageView.setVisibility(View.GONE);
		}
        
        intiButton(R.id.view_news, url);
        View candleButton = findViewById(R.id.back_list);
        candleButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				onBackPressed();
			}
        });
	}
	
	private void intiButton(final int id, final String url) {
		View view = findViewById(id);
		view.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				Intent i = new Intent(Intent.ACTION_VIEW);
				i.setData(Uri.parse(url));
				startActivity(i);
				finish();
			}
		});
	}
	
	public void onBackPressed() {
		finish();
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}
